﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DAMS.Models;
using DAMS.Service;
using DAMS.Web.Areas.Manage.Models;
using DAMS.Web.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace DAMS.Web.Areas.Manage.Controllers
{
    [Area("Manage")]
    public class TagController : Controller
    {
        private readonly IRepository _repo;
        private readonly SiteConfig _config;

        public TagController(IRepository repository,  IOptions<SiteConfig> option)
        {
            this._repo = repository;
            this._config = option.Value;
        }

        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 新建标签
        /// </summary>
        /// <param name="full">full参数为不使用Layout</param>
        /// <returns></returns>
        public IActionResult Create([FromQuery]bool full=false)
        {
            if (full)
                return View("Create_full");
            else
                return View();

        }
        [HttpPost]
        public IActionResult Create([FromForm]Tag tag)
        {
            Result<Tag> result= _repo.TagCreate(tag);
            if (result.Succeeded)
                return RedirectToRoute(new { Action = "List" });
            else
            {
                ErrorViewModel errvm = new ErrorViewModel();
                errvm.Redirect = Url.Action("Create");
                errvm.ErrorMsg = string.Join("<br />", result.Error);
                return View("Error", errvm);
            }
        }

        /// <summary>
        /// 远程验证标签名称
        /// </summary>
        /// <param name="TagName"></param>
        /// <returns></returns>
        [AcceptVerbs("Get", "Post")]
        public IActionResult Verify(string TagName)
        {
            if (_repo.TagGet(TagName).Succeeded)
            {
                return Json($"标签： {TagName} 已经存在");
            }

            return Json(true);
        }

        public IActionResult List([FromQuery]int pageIndex=1,[FromQuery]bool full=false, [FromQuery]int pageSize = 30,[FromQuery]string keyword="")
        {
            int tagCount = _repo.TagGetCount(keyword);
            if ((pageIndex - 1) * pageSize > tagCount)
                pageIndex = (int)Math.Ceiling(tagCount / (double)pageSize);
            IEnumerable <Tag> tags= _repo.TagGetListByPage(pageIndex, pageSize,keyword);
            ViewData["TagCount"] = tagCount;
            ViewData["PageSize"] = pageSize;
            ViewData["PageIndex"] = pageIndex;
            ViewData["keyWord"] = keyword;
            if (full)
                return View("List_full",tags);
            else
                return View(tags);
        }

        public IActionResult Delete([FromQuery]int id,[FromQuery]string tagname)
        {
            Result<Tag> result= _repo.TagDeleteByID(id);
            JumpViewModel jvm = new JumpViewModel()
            {
                Message =$"(标签ID:{id},标签名称:{tagname})" + (result.Succeeded ? result.SucceededMsg : string.Join("<br />", result.Error))  ,
                Title = "删除标签",
                ReturnUrl = Url.Action("List"),
                Time = 4
            };

            return View("Jump",jvm);
        }
        [HttpPost]
        public IActionResult Edit(Tag tag)
        {
            
            Result<Tag> result = _repo.TagUpdate(tag);
            JumpViewModel jvm = new JumpViewModel()
            {
                Message = $"(标签ID:{tag.ID},标签名称:{tag.TagName})" + (result.Succeeded ? result.SucceededMsg : string.Join("<br />", result.Error)),
                Title = "编辑标签",
                ReturnUrl = Url.Action("List"),
                Time = 0
            };

            return View("Jump", jvm);
        }

        public IActionResult Edit([FromQuery]int id, [FromQuery]string tagname)
        {
            TagCreateViewModel tag = new TagCreateViewModel();
            tag.ID = id;
            tag.TagName = tagname;
            return View(tag);
        }
    }
}